# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from tortoise import fields
from kernels.model import DbModel


class VideoRecordsModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    code = fields.CharField(null=False, default="", max_length=32, description="编号")
    type = fields.CharField(null=False, default="", max_length=20, description="类型: [text2video, image2video]")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    task_id = fields.CharField(null=False, default="", max_length=100, description="任务ID")
    engine = fields.CharField(null=False, default="", max_length=100, description="视频引擎")
    channel = fields.CharField(null=False, default="", max_length=32, description="模型渠道")
    model = fields.CharField(null=False, default="", max_length=32, description="适配模型")
    scale = fields.CharField(null=False, default="", max_length=20, description="视频比例")
    prompt = fields.TextField(null=False, description="视频描述")
    image_url = fields.CharField(null=False, default="", max_length=250, description="开始图片")
    image_end_url = fields.CharField(null=False, default="", max_length=250, description="结束图片")
    cover_url = fields.CharField(null=False, default="", max_length=250, description="封面图片")
    video_url = fields.CharField(null=False, default="", max_length=250, description="视频地址")
    use_points = fields.IntField(null=False, default=0, description="消耗积分")
    retry_count = fields.IntField(null=False, default=0, description="重试次数")
    fail_reason = fields.TextField(null=False, description="失败原因")
    notify_snap = fields.TextField(null=False, description="通知快照")
    ip = fields.CharField(null=False, default="", max_length=100, description="来源IP")
    status = fields.SmallIntField(null=False, default=0, description="生成状态: [0=等待中, 1=执行中, 2=成功, 3=失败]")
    is_free = fields.SmallIntField(null=False, default=0, description="会员免费: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    start_time = fields.IntField(null=False, default=0, description="开始时间")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "视频记录表"
        table = DbModel.table_prefix("video_records")


class VideoSquareModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    source = fields.IntField(null=False, default=0, description="来源: [1=官方, 2=用户]")
    operate_id = fields.IntField(null=False, default=0, description="操作人ID")
    cate_id = fields.IntField(null=False, default=0, description="分类ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    video_code = fields.CharField(null=False, default="", max_length=32, description="视频编号")
    video_url = fields.CharField(null=False, default="", max_length=250, description="视频地址")
    prompt = fields.TextField(default="", description="输入提示词")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    public_sum = fields.IntField(null=False, default=1, description="公开次数")
    audit_result = fields.CharField(null=False, max_length=300, default="", description="审核结果")
    audit_status = fields.SmallIntField(null=False, default=0, description="审核状态: [0=待审, 1=审核通过, 2=审核拒绝]")
    is_enable = fields.SmallIntField(null=False, default=0, description="是否显示: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "视频广场表"
        table = DbModel.table_prefix("video_square")

    @classmethod
    async def querySquareDictsByRecordId(cls, ids: list, field: list = None) -> dict:
        """ 获取广场字典 """
        dicts = {}
        if ids:
            field = cls.without_field("is_delete,delete_time") if not field else field
            result = await (cls.filter(record_id__in=list(set(ids)), is_delete=0).all().values(*field))
            dicts = {item["record_id"]: item for item in result}
        return dicts


class VideoCollectModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    total_num = fields.IntField(null=False, default=0, description="操作次数")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "视频收藏表"
        table = DbModel.table_prefix("video_collect")

